<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<script type="text/javascript">
$(function() {
	var urlPrefix = "${pageContext.request.contextPath}/api/";
	var searchCondition = {};
	searchCondition.member_login_name = "";
	searchCondition.enterprise_name = "";
	searchCondition.enterprise_property = "";
	searchCondition.enterprise_category = "";
	
	$('#member_manage_toolbar #member_login_name').textbox({
		height: 28,
		width: 150,
		value: searchCondition.member_login_name,
		onChange: function(newValue, oldValue) {
            searchCondition.member_login_name = newValue;
        }
	});
	
	$('#member_manage_toolbar #enterprise_name').textbox({
		height: 28,
		width: 150,
		value: searchCondition.enterprise_name,
		onChange: function(newValue, oldValue) {
            searchCondition.enterprise_name = newValue;
        }
	});
	
	$.codeNameCombo({
        urlPrefix: urlPrefix,
        target: $('#member_manage_toolbar #enterprise_property'),
        typeId: '02',
        required: false,
        editable: false,
        errorTitle: '<s:text name="title.error" />',
        errorMessage: '数据取得失败',
        blank: true,
        blankText: '全部',
        value: searchCondition.enterprise_property,
        onChange: function(newValue, oldValue) {
            searchCondition.enterprise_property = newValue;
        }
    });
	
	$.codeNameCombo({
        urlPrefix: urlPrefix,
        target: $('#member_manage_toolbar #enterprise_category'),
        typeId: '03',
        required: false,
        editable: false,
        errorTitle: '<s:text name="title.error" />',
        errorMessage: '数据取得失败',
        blank: true,
        blankText: '全部',
        value: searchCondition.enterprise_category,
        onChange: function(newValue, oldValue) {
            searchCondition.enterprise_category = newValue;
        }
    });
	
	$('#member_manage_grid').datagrid({
        url: '${pageContext.request.contextPath}/api/MemberSearchAPI.json',
        columns: [[
            {field:'member_login_name', title:'会员名', width:145, align:'left', halign:'center', resizable:false},
            {field:'enterprise_name', title:'企业名称', width:240, align:'left', halign:'center', resizable:false},
            {field:'enterprise_property_name', title:'企业性质', width:80, align:'center', halign:'center', resizable:false},
            {field:'enterprise_category_name', title:'行业分类', width:80, align:'center', halign:'center', resizable:false},
            {field:'mobile', title:'手机号码', width:130, align:'left', halign:'center', resizable:false},
            {field:'email', title:'电子邮件', width:200, align:'left', halign:'center', resizable:false},
            {field:'points', title:'会员积分', width:80, align:'right', halign:'center', resizable:false},
            {field:'rank_name', title:'会员等级', width:90, align:'center', halign:'center', resizable:false},
            {field:'opt', title:'操作', width:135, align:'center', halign:'center', resizable:false, formatter: function(value, row, index) {
            	return '<a class="member_manage_button_detail" href="#">查看</a>&nbsp;<a id="btn_member_delete" class="member_manage_button_delete" href="#">删除</a>';
            }}
        ]],
        fitColumns : false,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        autoRowHeight: true,
        idField: 'member_login_name',	     
        toolbar: '#member_manage_toolbar',
        pagination : true,
        pageSize: 10,
        pageList: [10],
		onSelect : function (rowIndex, rowData){
			$("#member_manage_grid").datagrid('unselectRow', rowIndex);
		},
        loadFilter: function(data) {	          
            var datax = {};
	        if(data.status!=0){
            	datax.total = 0;
            	datax.rows = new Array();
            	return datax;
            }
            
	        if (data.total && data.total > 0) {
	            datax.total = data.total;
	            datax.rows = data.members;		           
	        }else{
	        	datax.total = 0;
            	datax.rows = new Array();
	        }
	        return datax;
        },
        queryParams: {
            param: JSON.stringify(searchCondition)
        },
        onLoadSuccess: function(data) {
        	if (data.total > 0) {
            	$('.member_manage_button_detail').linkbutton({
                    iconCls: 'icon-search',
                    onClick: function() {
                    	var index = $(this).index('.member_manage_button_detail');	                    	
                        var memberData = $('#member_manage_grid').datagrid('getData').rows[index];	                    	
            			var memberLoginName = memberData.member_login_name; 
            			$.showMemberDetailWindow(memberLoginName);
                    }
                });
                
                $('.member_manage_button_delete').linkbutton({
                    iconCls: 'icon-delete',
                    onClick: function() {	                    	
                    	var index = $(this).index('.member_manage_button_delete');
                        var memberData = $('#member_manage_grid').datagrid('getData').rows[index];	                    	
            			var memberLoginName = memberData.member_login_name; 
        			    $.messager.confirm('确认', '您确定要删除会员 ' + memberData.member_login_name + ' 吗？', function(r){
                      	  if (r == true){
                      		 deleteMember(memberLoginName);
                      	  }
                        });
                    }
                });
                
                $('#member_manage_grid').datagrid('fixRowHeight');
            }
        }
    });
	
	function initCondition() {
	    searchCondition.member_login_name = "";
		searchCondition.enterprise_name = "";
		searchCondition.enterprise_property = "";
		searchCondition.enterprise_category = "";
		
		$('#member_manage_toolbar #member_login_name').textbox('clear');
		$('#member_manage_toolbar #enterprise_name').textbox('clear');
		$('#member_manage_toolbar #enterprise_property').combobox('clear');
		$('#member_manage_toolbar #enterprise_category').combobox('clear');
	}

	function deleteMember(memberLoginName){		
		//删除会员信息
    	var memberDeleteAPI = new MemberDeleteAPI({
	        urlPrefix : urlPrefix
	    });
    	var paramData = {};
    	paramData.memberLoginName = memberLoginName;    	    	
    	memberDeleteAPI.del({
            onSuccess : function(data) {            	
            	$('#member_manage_grid').datagrid("reload", data);  
            },
            onAPIError : function(status, errors) {
                
            },
            onServerError : function(jqXHR, textStatus, errorThrown) {
            	
            }
        },       
        {
            param: JSON.stringify(paramData)
        });
    }
	
	$('#member_manage_toolbar #btn_member_search').linkbutton({
	    onClick: function() {
	        $('#member_manage_grid').datagrid({
	            pageNumber: 1,
	    	    queryParams: {
	                param: JSON.stringify(searchCondition)
	            }
	    	});
	    }
	});
	
	$('#member_manage_toolbar #btn_member_reset').linkbutton({
	    onClick: function() {
	        initCondition();
	        $('#member_manage_grid').datagrid({
	            pageNumber: 1,
	    	    queryParams: {
	                param: JSON.stringify(searchCondition)
	            }
	    	});
	    }
	});
});
</script>
<div id="member_manage_toolbar" style="padding: 5px 10px;">
  <span>会员名：</span><input type="text" id="member_login_name">
  <span>企业名称：</span><input type="text" id="enterprise_name">
  <span>企业性质：</span><input type="text" id="enterprise_property" style="width: 120px; height: 28px;">
  <span>行业分类：</span><input type="text" id="enterprise_category" style="width: 120px; height: 28px;">
  <a id="btn_member_reset" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-back'" style="width:80px">重置</a>
  <a id="btn_member_search" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" style="width:80px">查询</a>
</div>
<table id="member_manage_grid"></table>